var routes = [

	{
		path: '/',
		name: 'layout',
		redirect: {
			name: "index"
		},
		// component: () => import('@/views/layout.vue'),
		component: "layout", //通过名称去找到对应的路由
		children: [{
				// path: "/index",
				// name: "index",
				// component: () => import("@/views/index/index.vue")
				meta:{title:"首页"},
				component: 'index/index'
			},
			{
				meta:{title:"商品列表"},
				component: "shop/goods/list"
			},
			{
				meta:{title:"商品相册"},
				component: "image/index"
			}
		]
	},
	{
		path: "/login",
		name: "login",
		meta:{title:"登录页"},
		// component: () => import('@/views/login/index.vue')
		component: 'login/index'
	},
	//=>兜底路由
	{
		path: "*",
		redirect: {
			name: "index"
		}
	}

]

//获取路由信息的方法
let getRoutes = function () {
	createRoutes(routes)
	return routes;
}
//自动生成路由的方法(到时方便递归)
function createRoutes(arr) {
	for (let i = 0; i < arr.length; i++) {
		//判断有没有component
		if (arr[i].component) {
			//判断有没有index
			let val = getValue(arr[i].component)

			arr[i].name= arr[i].name||val.replace(/\//g,'_')  //有默认值就取默认的没有就取最新的
			
			arr[i].path= arr[i].path||`/${val}`

			//拼接
			let componentFun = import(`@/views/${arr[i].component}.vue`)
			//把component设置成()=>import("fsfdsfs")
			arr[i].component = () => componentFun
			if (arr[i].children && arr[i].children.length > 0) {
				//如果还有children就继续递归
				createRoutes(arr[i].children)
			}

		}

	}
}


//去除index的方法
function getValue(str) {

	let index = str.lastIndexOf("/")

	let val = str.substring(index, str.length)
	
	if (val === "/index") {

		return str.substring(index, -1) //往前面截取

	} else {
		return str
	}

}


export default getRoutes()